import { Tree } from 'antd';
import React, { FC, useEffect, useState } from 'react';
import { request } from 'umi';
interface TreeProps {
    value?: string,
    onChange?: (value: string) => void,
    disabled?: boolean
}
const SelectJdTree: FC<TreeProps> = (props: TreeProps) => {


    const [expandedKeys, setexpandedKeys] = useState(['44', '4403']);
    const {
        value,
        onChange,
        disabled = false
    } = props;
    const [treeData, settreedata] = useState([]);
    const getdata = () => {
        request('/data/dynamic/query', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            data: {
                apiID: 'ADR_GOVER_001'
            }
        }).then((res) => {
            let definedkey = ['440307009', '440307010', '440307011']//特殊处理3个街道
            let definedAObj = {};
            let data = res?.data?.data;
            let obj = {};
            data.sort((a, b) => {
                return a.div_level - b.div_level;
            })
            let level1, level2, level3;
            data.forEach((item) => {
                switch (item.div_level) {
                    case 1:
                        obj[item?.adm_division] = {
                            detail: item
                        };;
                        break;
                    case 2:
                        level1 = item?.adm_division.substring(0, 2);
                        if (obj[level1]) {
                            obj[level1][item?.adm_division] = {
                                detail: item
                            };
                        }
                        //判断keys 中存在item.adm_division

                        break;
                    case 3:
                        level1 = item?.adm_division.substring(0, 2);
                        level2 = item?.adm_division.substring(0, 4);
                        obj[level1][level2][item?.adm_division] = {
                            detail: item
                        };

                        break;
                    case 4:
                        level1 = item?.adm_division.substring(0, 2);
                        level2 = item?.adm_division.substring(0, 4);
                        level3 = item?.adm_division.substring(0, 6);
                        if (definedkey.indexOf(item?.adm_division) != -1) {
                            // obj[level1][level2][level3][item?.adm_division] = {
                            //     detail: item
                            // };
                            definedAObj[item?.adm_division] = {
                                detail: {
                                    ...item,
                                    ...{
                                        'up': { div_level: 3, adm_division: '999999', adm_division_name: '大鹏新区' }
                                    }
                                },

                            };
                        } else {
                            obj[level1][level2][level3][item?.adm_division] = {
                                detail: {
                                    ...item,
                                    ...{
                                        'up': obj[level1][level2][level3]['detail']
                                    }
                                }
                            };
                        }

                        break;
                    default:
                        break;
                }
            });
            console.log(obj);
            obj['44']['4403']['999999'] = {
                ...definedAObj,
                ...{
                    detail: { div_level: 3, adm_division: '999999', adm_division_name: '大鹏新区' }
                }
            }
            let arr = handleData(obj);
            console.log(arr);
            settreedata(arr);
        });
    };
    const handleData = (obj) => {
        let arr = [];
        for (let i in obj) {
            if (i != 'detail' && i != 'up') {
                let keys = Object.keys(obj[i]);
                let item = {
                    title: obj[i]['detail']['adm_division_name'],
                    key: obj[i]['detail']['adm_division'],
                    isdisabled: keys.length > 1,
                    up: obj[i]['detail']['up']
                };
                if (keys.length > 1) {
                    item['children'] = handleData(obj[i]);
                }
                arr.push(item);
            }

        }
        return arr;
    };

    useEffect(() => {
        getdata();
    }, []);
    return <div >
        {
            treeData.length > 0 && <Tree
                treeData={treeData}
                disabled={disabled}
                selectedKeys={[value]}
                expandedKeys={expandedKeys}
                onSelect={(selectedKeysn, e) => {
                    let selectid = e?.node?.key;
                    if (!e.node.isdisabled) {
                        props.onChange && props.onChange(selectedKeysn[0], e);
                    } else {
                        if (expandedKeys.indexOf(selectid) == -1) {
                            setexpandedKeys(expandedKeys.concat(selectid))
                        } else {
                            let expandedKeysn = expandedKeys.concat([]);
                            // 从数组expandedKeysn中删除e
                            let index = expandedKeysn.indexOf(selectid);
                            if (index > -1) {
                                expandedKeysn.splice(index, 1);
                            }

                            setexpandedKeys(expandedKeysn)
                        }
                    }

                }}
                onExpand={(expandedKeysn) => {
                    console.log(expandedKeys)
                    setexpandedKeys(expandedKeysn)
                }}
            />
        }


    </div >
}


export default SelectJdTree;
